package com.yzt.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzt.base.kit.Base;
import com.yzt.base.utils.StringUtils;
import com.yzt.sys.entity.User;
import com.yzt.sys.kit.SysKit;
import com.yzt.sys.service.IUserService;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Y
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/sys/user")
public class UserController {

	@Autowired
	private IUserService userService;

	@PostMapping("")
	@ApiOperation("新增系统管理员账号")
	public Base<Object> save(User user) {
		
		if(userService.exit(user.getUsername())) {
			return Base.fail("用户名重复");
		}
		
		user.setPassword(StringUtils.encoder(user.getPassword()));
		if (userService.save(user)) {
			return Base.ok();
		}
		return Base.fail();
	}

	@GetMapping("{id}")
	@ApiOperation("获取管理员账号信息")
	public Base<User> get(@PathVariable("id") Long id) {
		User u = userService.getById(id);
		if (u != null)
			return Base.okData(u);
		return Base.fail();
	}

	@PutMapping("")
	@ApiOperation("更新管理员账号信息")
	public Base<Object> update(User user) {
		
		if (StringUtils.isNotBlank(user.getPassword())) {
			user.setPassword(StringUtils.encoder(user.getPassword()));
		}else {
			user.setPassword(null);
		}
		if (userService.updateById(user)) {
			return Base.ok();
		}
		return Base.fail();
	}

	@DeleteMapping("{id}")
	@ApiOperation("删除管理员账号")
	public Base<Object> del(@PathVariable("id") Long id) {
		if (userService.removeById(id))
			return Base.ok();
		return Base.fail();
	}

	@PostMapping("disable/{id}")
	@ApiOperation("禁用账户")
	public Base<Object> disable(@PathVariable("id") Long id) {
		User u = new User();
		u.setId(id);
		u.setStatus("1");
		if (userService.updateById(u))
			return Base.ok();
		return Base.fail();
	}

	@PostMapping("enable/{id}")
	@ApiOperation("启用账户")
	public Base<Object> enable(@PathVariable("id") Long id) {
		User u = new User();
		u.setId(id);
		u.setStatus("0");
		if (userService.updateById(u))
			return Base.ok();
		return Base.fail();
	}

	@GetMapping("list")
	@ApiOperation("管理员账户列表")
	public Base<PageInfo<User>> list(SysKit kit) {
		PageInfo<User> list = PageHelper.startPage(kit.getPage(), kit.getLimit())
				.doSelectPageInfo(() -> userService.list(
						Wrappers.<User>lambdaQuery().eq(org.apache.commons.lang3.StringUtils.isNotBlank(kit.getName()),
								User::getNikename, kit.getName())));
		return Base.okData(list);
	}

}
